package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.system.Permission;
import com.ihrm.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/sys/permission")
public class PermissionController extends BaseController {

	@Autowired
	private PermissionService permissionService;

	/**
	 * 添加权限
	 */
	@PostMapping
	public Result add(@RequestBody Map<String,Object> map) throws Exception {
		permissionService.save(map);
		return Result.SUCCESS();
	}

	/**
	 * 根据id查询权限
	 */
	@GetMapping("/{permissionId}")
	public Result findById(@PathVariable(value = "permissionId") String id) throws Exception {
		Map<String,Object> map = permissionService.findById(id);
		return new Result(ResultCode.SUCCESS,map);
	}

	/**
	 * 根据id更新权限
	 */
	@PutMapping("/{permissionId}")
	public Result update(@PathVariable(value = "permissionId") String id, @RequestBody Map<String,Object> map) throws Exception {
		map.put("id",id);
		permissionService.update(map);
		return Result.SUCCESS();
	}

	/**
	 * 查询所有权限列表
	 */
	@GetMapping
	public Result findAll(@RequestParam Map< String,Object> map) {

		List<Permission> list= permissionService.findAll(map);
		//返回结果
		return new Result(ResultCode.SUCCESS, list);
	}

	/**
	 * 根据id删除权限
	 */
	@DeleteMapping("/{permissionId}")
	public Result delete(@PathVariable(value = "permissionId") String id) throws Exception {
		permissionService.delete(id);
		return Result.SUCCESS();
	}
}
